package org.com.testpro.Dao;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.com.testpro.Bean.RedisHbaseVuale;
import org.com.testpro.StringUtil.Datautil;
import org.com.testpro.StringUtil.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 二期radis操作
 * 
 * @author ycq
 *
 */
@Component
public class RadisDataJQ {

	
	@Autowired
	StringUtil StringUtil;
	@Autowired
	org.springframework.data.redis.core.StringRedisTemplate StringRedisTemplate;

	/**
	 * 获取某个设备的最新一条信息
	 * 
	 * @param gateway
	 * @param modular
	 * @param pathn
	 * @return
	 */
	public Map<String, String> getEquipData(final String gateway,final String modular, final String pathn) {
		
		String key = String.format("%s_%s", gateway, modular+"_"+pathn);
		 key = String.format("%s-[%s]", "BSD", key);
		final Map<String, String> data = new HashMap<String, String>();
		Set<String> vaules = StringRedisTemplate.opsForZSet().range(key, 0, 0);
		if(vaules.size()==1)
		{
			for (String string : vaules) {
				String[]  item_values = string.split("_");
				data.put("time", item_values[0]);
				data.put("value", item_values[1]);
				break;
			}
		}
		else {
			
			System.err.println("取值数据不是一个"+vaules.size());
			System.err.println(key);
		}
		return data;
	}

	/**
	 * 
	 * @param gateway
	 *            网关号
	 * @param modular
	 *            模块号
	 * @param pathn
	 *            通道号
	 * @return HashMap<String, Integer>
	 * @Description: 返回 一个设备 一秒传输了多少个数据
	 */
	public HashMap<String, Integer> getEquipData1000_Ext(final String gateway,
			final String modular, final String pathn) {
		final HashMap<String, Integer> data = new HashMap<String, Integer>();
		String key = String.format("%s_%s", gateway, modular+"_"+pathn);
		 key = String.format("%s-[%s]", "BSD", key);

		Integer showNums = 1;
		String[] item_values = null;
//		Set<String> vaules =CacheManager.execute(BridgeRadisCacheHandler); 
		Set<String> vaules = StringRedisTemplate.opsForZSet().range(key, 0, 1000);

		String timeSeq = null;
		String value=null;
		for (String item : vaules) {
			
			item_values = item.split("_");
			timeSeq = Datautil.LongTimeStampTODate(Long.parseLong(item_values[0]),"yyyy:MM:dd:HH:mm:ss");
			value=item_values[1];
			System.out.println("time"+"--"+timeSeq+"--"+"vaule"+value);
			if (data.containsKey(timeSeq)) {
				showNums = data.get(timeSeq);
				if (null != showNums) {
					data.put(timeSeq, showNums + 1);
				} else {
					data.put(timeSeq, 1);
				}
			} else {
				data.put(timeSeq, 1);
			}
		}
		return data;
	}
	
	
	
	public LinkedHashMap<String, RedisHbaseVuale> getEquipData1000_ExtForlist( String gateway,
			final String modular, final String pathn) {
		
			gateway=StringUtil.Bridgenamechangegetawy(gateway);
		final LinkedHashMap<String, RedisHbaseVuale> data = new LinkedHashMap<String, RedisHbaseVuale>();
		String key = String.format("%s_%s", gateway, modular+"_"+pathn);
		 key = String.format("%s-[%s]", "BSD", key);
		String[] item_values = null;
		Set<String> vaules = StringRedisTemplate.opsForZSet().range(key, 0, 1000);

		if(vaules.isEmpty()){return null;}
		String timeSeq = null;
		String value=null;
		for (String item : vaules) {
			item_values = item.split("_");
			timeSeq=item_values[0];
			value=item_values[1];
			data.put(timeSeq, new RedisHbaseVuale(null, value, false));
		}
		return data;
	}
	
	
	
	
	
	
}
